package com.pedro.streamer.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.pedro.streamer.BmiBaseApp;
import com.pedro.streamer.R;
import com.pedro.streamer.manager.MediaPlayerClient;
import com.pedro.streamer.utils.ThreadUtils;


public class AudioControlView extends ConstraintLayout implements View.OnClickListener {

    private static final String TAG = "AudioControlView";
    private TextView fileNameTv, musicNameTv;
    private ImageView preBtn, playPauseBtn, nextBtn;
    private SeekBar supVolumeBar, supDelayTimeBar;
    private TextView delayTimeTv, supVolumeTv;
    private LinearLayout listPlayView, randomPlayView;
    public AudioControlView(@NonNull Context context) {
        super(context);
    }

    public AudioControlView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        initView();
    }

    private void initView() {
        fileNameTv = findViewById(R.id.file_name);
        musicNameTv = findViewById(R.id.music_name);
        preBtn = findViewById(R.id.pre_icon);
        preBtn.setOnClickListener(this);
        playPauseBtn = findViewById(R.id.playpause_icon);
        playPauseBtn.setOnClickListener(this);
        nextBtn = findViewById(R.id.next_icon);
        nextBtn.setOnClickListener(this);
        listPlayView = findViewById(R.id.mode_l);
        listPlayView.setOnClickListener(this);
        randomPlayView = findViewById(R.id.mode_r);
        randomPlayView.setOnClickListener(this);
        supVolumeBar = findViewById(R.id.sup_volume_seek_bar);
        supVolumeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "音量: " + seekBar.getProgress()/100.00f);
                MediaPlayerClient.getInstance(BmiBaseApp.getInstance().getApplicationContext()).updateMusicVolume(seekBar.getProgress()/100.0f);
                supVolumeTv.setText(String.valueOf(seekBar.getProgress()));
            }
        });
        supDelayTimeBar = findViewById(R.id.sup_delay_seek_bar);
        supDelayTimeBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//                delayTimeTv.setText(String.valueOf(i));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Log.d(TAG, "延时: " + seekBar.getProgress() * 1000);
                MediaPlayerClient.getInstance(BmiBaseApp.getInstance().getApplicationContext()).setDelayTime(seekBar.getProgress() * 1000);
                delayTimeTv.setText(String.valueOf(seekBar.getProgress()));
            }
        });
        delayTimeTv = findViewById(R.id.delay_time);
        supVolumeTv = findViewById(R.id.sup_volume_tv);
    }

    @Override
    public void onClick(View view) {

        int id = view.getId();
        if (id == R.id.pre_icon) {
            Log.d(TAG, "onClick: pre");
            MediaPlayerClient.getInstance(BmiBaseApp.getInstance().getApplicationContext()).playPre();
        } else if (id == R.id.playpause_icon) {
            Log.d(TAG, "onClick: playpause");
            MediaPlayerClient.getInstance(BmiBaseApp.getInstance().getApplicationContext()).playRandomSong(!MediaPlayerClient.getInstance(BmiBaseApp.getInstance().getApplicationContext()).isSupPlaying());
        } else if (id == R.id.next_icon) {
            Log.d(TAG, "onClick: next");
            MediaPlayerClient.getInstance(BmiBaseApp.getInstance().getApplicationContext()).playNext();
        } else if (id == R.id.mode_l) {
            Log.d(TAG, "顺序播放");
            MediaPlayerClient.getInstance(BmiBaseApp.getInstance().getApplicationContext()).setPlayMode(MediaPlayerClient.PlayMode.ListPlay);
        } else if (id == R.id.mode_r) {
            Log.d(TAG, "随机播放");
            MediaPlayerClient.getInstance(BmiBaseApp.getInstance().getApplicationContext()).setPlayMode(MediaPlayerClient.PlayMode.RandomPlay);
        }

    }

    public void updateMusicTitle(String musicName, String fileName) {
        ThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                fileNameTv.setText(fileName);
                musicNameTv.setText(musicName);
            }
        });

    }

    public void updatePlayMode(MediaPlayerClient.PlayMode playmode) {
        Log.d(TAG, "updatePlayMode: 刷新播放模式UI");
        if (playmode == MediaPlayerClient.PlayMode.RandomPlay) {
            listPlayView.setBackgroundResource(R.drawable.mode_selector_l);
            randomPlayView.setBackgroundResource(R.drawable.mode_selector_r_s);
        } else {
            listPlayView.setBackgroundResource(R.drawable.mode_selector_l_s);
            randomPlayView.setBackgroundResource(R.drawable.mode_selector_r);
        }
    }

    public void updateIsPlaying(boolean isPlaying) {
        if (isPlaying) {
            playPauseBtn.setImageResource(R.drawable.pause);
        } else {
            playPauseBtn.setImageResource(R.drawable.play);
        }
    }

    public void updateSupVolume(int volume) {
        Log.d(TAG, "updateSupVolume: " + volume);
        supVolumeBar.setProgress(volume);
        supVolumeTv.setText(String.valueOf(volume));
    }

    public void updateDelayTime(long delayTime) {
        supDelayTimeBar.setProgress((int) (delayTime / 1000));
        delayTimeTv.setText(String.valueOf((int) (delayTime / 1000)));
    }
}
